package com.three.productList.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.Product;
import com.three.core.entity.ProductReview;
import com.three.core.vo.R;
import com.three.productList.dao.ProductReviewDao;
import com.three.productList.service.ProductReviewService;
import com.three.productList.vo.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：li
 * @date ：Created in 2025/3/13 013 16:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductReviewServiceImpl implements ProductReviewService {
    @Autowired
    private ProductReviewDao productReviewDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    //分页查询
    @Override
    public R<IPage<ProductReview>> selectProductReview(SearchFormReview searchForm) {
        //获取用户信息
        String token=request.getHeader("token");
        String name = (String) redisTemplate.opsForHash().get("user::login::" + token, "name");
        if (!name.equals("admin")){
            searchForm.setUserName(name);
        }
        Page<ProductReview> page = new Page<>(searchForm.getCurrentPage(),searchForm.getPageSize());
        IPage<ProductReview> products = productReviewDao.selectMyList(page, searchForm);
        return new R<IPage<ProductReview>>(200,"查询成功",products);
    }
    @Override
    public R<List<EvaluateVo>> selectCommentsVoByProductId(Integer productId) {
        List<EvaluateVo> evaluateVos = productReviewDao.selectEvaluateVoByProductId(productId);
        return new R<>(200,"查询成功",evaluateVos);
    }

    @Override
    public R<List<ProductReview>> selectCommentsByProductId(Integer productId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",productId);
        List<ProductReview> productReviews = productReviewDao.selectList(queryWrapper);
        return new R<>(200,"查询成功",productReviews);
    }
    //查询商品好评率和评论量
    @Override
    public R<ProductRatVo> selectContentByProductId(Integer productId) {
        QueryWrapper<ProductReview> queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",productId);
        List<ProductReview> productReviews = productReviewDao.selectList(queryWrapper);
        if (productReviews.size()==0){
            return new R<>(200,"查询成功",null);
        }
        ProductRatVo productRatVo = new ProductRatVo();
        //统计评论量
        int total = 0;
        for (ProductReview productReview : productReviews) {
            if (productReview.getRating()==5){
                total++;
            }
        }
        productRatVo.setTotal(productReviews.size());
        productRatVo.setGoodRatingsPercentage(total/productReviews.size()*100);
        return new R<>(200,"查询成功",productRatVo);
    }
}
